﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using OnlineManagement.Model;
namespace OnlineManagement.Web.WebServices
{
    /// <summary>
    /// HybridizationRecordService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class HybridizationRecordService : System.Web.Services.WebService
    {
        [WebMethod]
        public Model.SowInfoForHybrizationRecord GetBasicSowInfo(int hoggeryId, Guid sowId)
        {
            string json = string.Empty;
            BLL.Dal.HybridizationRecord bllhr = new BLL.Dal.HybridizationRecord();
            Model.SowInfoForHybrizationRecord record = bllhr.GetHybridizationSowBasicInfo(hoggeryId, sowId);
            return record;
        }

        [WebMethod(EnableSession = true)]
        public string GetYuChanDate(DateTime hybridizationDate) {
          return  Common.YuChanHelper.GetYuChanDate(hybridizationDate).ToString("yyyy-MM-dd");
        }

        [WebMethod(EnableSession = true)]
        public JsonMsg HybridizationAddOrUpdate
            (int hoggeryID,
            Guid sowId,
            string hybridizationDateStr,
            string actionPerson,
            string boarBitID,
            Guid boarPigType,
            string comment) 
        {
            JsonMsg msg = new JsonMsg();
            if(string.IsNullOrEmpty(boarBitID)||string.IsNullOrEmpty(hybridizationDateStr)){
                msg.Flag = false;
                msg.Message = Common.PubMsg.InValidateData;
                return msg;
            }
            DateTime hybridizationDate;
            if(!DateTime.TryParse(hybridizationDateStr,out hybridizationDate)){
                msg.Flag = false;
                msg.Message = Common.PubMsg.InValidateData;
                return msg;
            }
            OnlineManagement.BLL.Dal.HybridizationRecord BllRecord = new BLL.Dal.HybridizationRecord();
            try
            {
                string returnValue = string.Empty;
                List<Model.HybridizationRecord> lists= new List<Model.HybridizationRecord>();
                lists.Add(new Model.HybridizationRecord(){
                     SowID=sowId,
                     HoggeryID=hoggeryID,
                     HybridizationDate=hybridizationDate,
                     HybridizationPerson=actionPerson,
                     BoarBitID=boarBitID,
                     BoarPigType=boarPigType,
                     HybridizationCount=1
                });
                bool result = BllRecord.SowHybrizationAddOrUpdate(lists, out returnValue);
                msg = new JsonMsg() { Flag = result, Message = returnValue };
                CreateHybridizationBusinessLog(hoggeryID, sowId,msg);
                return msg;
            }catch(Exception ex){
                msg = new JsonMsg() { Flag = false, Message = OnlineManagement.Common.PubConstant.ServerError };
                log4net.ILog log = log4net.LogManager.GetLogger(new System.Diagnostics.StackFrame(0).GetMethod().Name);
                log.Error(new Com.HoggeryLog() { HoggeryID=hoggeryID }, ex);
                return msg;
            }
        }

        

        [WebMethod(EnableSession = true)]
        public JsonMsg HybridizationAddOrUpdate2
            (int hoggeryID,
            Guid sowId,
            string hybridizationDateStr,
            string actionPerson,
            string boarBitID,
            Guid boarPigType,
            string comment,

            string secondHybridizationDateStr,
            string SecondPerson,
            string secondBoarBitID,
            Guid secondBoarPigType,
            string secondComment
            )
        {
            JsonMsg msg = new JsonMsg();
            if (string.IsNullOrEmpty(boarBitID) || string.IsNullOrEmpty(hybridizationDateStr))
            {
                msg.Flag = false;
                msg.Message = Common.PubMsg.InValidateData;
                return msg;
            }
            DateTime hybridizationDate;
            DateTime secondHybridizationDate;
            if (!DateTime.TryParse(hybridizationDateStr, out hybridizationDate) ||
                !DateTime.TryParse(secondHybridizationDateStr,out secondHybridizationDate))
            {
                msg.Flag = false;
                msg.Message = Common.PubMsg.InValidateData;
                return msg;
            }

            List<Model.HybridizationRecord> records = new List<HybridizationRecord>();
            records.Add(new Model.HybridizationRecord() {
                SowID=sowId,
                HoggeryID=hoggeryID,
                HybridizationDate=hybridizationDate,
                HybridizationPerson = actionPerson,
                BoarBitID=boarBitID,
                BoarPigType=boarPigType,
                Comment=comment,
                HybridizationCount=1
            });
           
            records.Add(new Model.HybridizationRecord() { 
                 SowID=sowId,
                 HoggeryID=hoggeryID,
                 HybridizationDate = secondHybridizationDate,
                 HybridizationPerson=SecondPerson,
                 BoarBitID=secondBoarBitID,
                 BoarPigType=secondBoarPigType,
                 Comment=secondComment,
                 HybridizationCount=2
            });
            try
            {
                BLL.Dal.HybridizationRecord hyBll = new BLL.Dal.HybridizationRecord();
                string returnValue = string.Empty;
                bool result = hyBll.SowHybrizationAddOrUpdate(records, out returnValue);
                msg = new JsonMsg() { Flag = result, Message = returnValue };
                CreateHybridizationBusinessLog(hoggeryID, sowId, msg);
                return msg;
            }catch(Exception ex){
                msg = new JsonMsg() { Flag = false, Message = OnlineManagement.Common.PubConstant.ServerError };
                log4net.ILog log = log4net.LogManager.GetLogger(new System.Diagnostics.StackFrame(0).GetMethod().Name);
                log.Error(new Com.HoggeryLog() { HoggeryID = hoggeryID }, ex);
                return msg;
            }
        }

        [WebMethod(EnableSession = true)]
        public JsonMsg HybridizationAddOrUpdate23
            (int hoggeryID,
            Guid sowId,
            string hybridizationDateStr,
            string actionPerson,
            string boarBitID,
            Guid boarPigType,
            string comment,

            string secondHybridizationDateStr,
            string SecondPerson,
            string secondBoarBitID,
            Guid secondBoarPigType,
            string secondComment,

            string thirdHybridizationDateStr,
            string thirdPerson,
            string thirdBoarBitID,
            Guid thirdBoarPigType,
            string thirdComment
            )
        {
            JsonMsg msg = new JsonMsg();
            if (string.IsNullOrEmpty(boarBitID) || string.IsNullOrEmpty(hybridizationDateStr))
            {
                msg.Flag = false;
                msg.Message = Common.PubMsg.InValidateData;
                return msg;
            }
            DateTime hybridizationDate;
            DateTime secondHybridizationDate;
            DateTime thirdHybridizationDate;
            if (!DateTime.TryParse(hybridizationDateStr, out hybridizationDate)
                ||!DateTime.TryParse(secondHybridizationDateStr, out secondHybridizationDate)
                ||!DateTime.TryParse(thirdHybridizationDateStr,out thirdHybridizationDate))
            {
                msg.Flag = false;
                msg.Message = Common.PubMsg.InValidateData;
                return msg;
            }

            List<Model.HybridizationRecord> records = new List<HybridizationRecord>();
            records.Add(new Model.HybridizationRecord()
            {
                SowID = sowId,
                HoggeryID = hoggeryID,
                HybridizationDate = hybridizationDate,
                HybridizationPerson = actionPerson,
                BoarBitID = boarBitID,
                BoarPigType = boarPigType,
                Comment = comment,
                HybridizationCount = 1
            });
            records.Add(new Model.HybridizationRecord()
            {
                SowID = sowId,
                HoggeryID = hoggeryID,
                HybridizationDate = secondHybridizationDate,
                HybridizationPerson = SecondPerson,
                BoarBitID = secondBoarBitID,
                BoarPigType = secondBoarPigType,
                Comment = secondComment,
                HybridizationCount = 2
            });
            records.Add(new Model.HybridizationRecord() { 
                 SowID=sowId,
                 HoggeryID=hoggeryID,
                 HybridizationDate=thirdHybridizationDate,
                 BoarBitID=thirdBoarBitID,
                 BoarPigType=thirdBoarPigType,
                 Comment=thirdComment,
                 HybridizationCount=3
            });
            try
            {
                BLL.Dal.HybridizationRecord hyBll = new BLL.Dal.HybridizationRecord();
                string returnValue = string.Empty;
                bool result = hyBll.SowHybrizationAddOrUpdate(records, out returnValue);
                msg = new JsonMsg() { Flag = result, Message = returnValue };
                CreateHybridizationBusinessLog(hoggeryID, sowId, msg);
                return msg;
            }catch(Exception ex){
                msg = new JsonMsg() { Flag = false, Message = OnlineManagement.Common.PubConstant.ServerError };
                log4net.ILog log = log4net.LogManager.GetLogger(new System.Diagnostics.StackFrame(0).GetMethod().Name);
                log.Error(new Com.HoggeryLog() { HoggeryID = hoggeryID }, ex);
                return msg;
            }
        }



       /// <summary>
       /// 母猪反情检查，怀孕前期进行母猪反情检查，如果反情，则母猪进入发情配种期，母猪反情记录累加1
       /// </summary>
       /// <param name="hoggeryId"></param>
       /// <param name="sowId"></param>
       /// <param name="isFanQing">0-成功怀孕，1-反情</param>
       /// <param name="updateTime"></param>
       /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public JsonMsg FanQingValidate(int hoggeryId, Guid sowId, int isFanQing, DateTime updateTime)
        {
            JsonMsg msg = new JsonMsg();
            log4net.ILog log4 = OnlineHoggeryLog.GetLog(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());
            try
            {
                BLL.Dal.HybridizationRecord bllHr = new BLL.Dal.HybridizationRecord();
                string returnValue = string.Empty;
                bool result = bllHr.SowPregnancyRecord(hoggeryId, sowId, isFanQing, updateTime, out returnValue);
                msg.Flag = result;
                msg.Message = returnValue;
                string logMsg = isFanQing==0?"成功怀孕":"母猪反情";
                
                Com.HoggeryLog HLlog = new Com.HoggeryLog()
                {
                    HoggeryID = hoggeryId,
                    PigId = sowId.ToString(),
                    LogType = Com.HoggeryLogType.FanQingCheck,
                    Message =logMsg,
                    BusinessLogDate = updateTime
                };
                log4.Info(HLlog);

            }
            catch (Exception ex)
            {
                log4.Error(new Com.HoggeryLog() { HoggeryID = hoggeryId }, ex);
                msg.Message = "服务器发生错误，请稍后再试。";
                msg.Flag = false;
            }
            return msg;
        }


        /// <summary>
        /// 母猪妊娠检查，怀孕期的母猪都可以进行妊娠检查，这里能标示母猪是否流产或者空怀
        /// </summary>
        /// <param name="hoggeryId"></param>
        /// <param name="sowId"></param>
        /// <param name="updateDate"></param>
        /// <returns></returns>
        [WebMethod(EnableSession = true)]
        public JsonMsg PregnancyValidate(int hoggeryId,Guid sowId,string reason, DateTime updateDate) {
            
            log4net.ILog log4 = OnlineHoggeryLog.GetLog(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString());
            BLL.Dal.HybridizationRecord dllHr = new BLL.Dal.HybridizationRecord();
            string returnValue=string.Empty;
            try
            {
                bool result = dllHr.SowAbortion(hoggeryId, sowId, out returnValue);
                JsonMsg msg = new JsonMsg() { Flag = result, Message = returnValue };
                Com.HoggeryLog businessLog = new Com.HoggeryLog()
                {
                    BusinessLogDate = updateDate,
                    HoggeryID = hoggeryId,
                    PigId = sowId.ToString(),
                    LogType = Com.HoggeryLogType.PregnancyCheck,
                    Message = "母猪流产",
                    LogComment = reason
                };
                log4.Info(businessLog);
                return msg;
            }
            catch (Exception ex)
            {
                log4.Error(new Com.HoggeryLog() { HoggeryID=hoggeryId}, ex);
                return new JsonMsg() {Flag=false, Message="服务器发生错误，请稍后再试。" };
            }
        }


        #region private method
        private static void CreateHybridizationBusinessLog(int hoggeryID, Guid sowId, JsonMsg msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(new System.Diagnostics.StackFrame(0).GetMethod().Name);

            Com.HoggeryLog businessLog = new Com.HoggeryLog()
            {
                HoggeryID = hoggeryID,
                PigId = sowId.ToString(),
                BusinessLogDate = System.DateTime.Now,
                LogType = Com.HoggeryLogType.Hybridization,
              
            };
            if (msg.Flag)
            {
                businessLog.Message = "配种成功";
            }
            else { businessLog.Message = "配种失败"; }
            businessLog.LogComment = msg.Message;
            log.Info(businessLog);
        }
        #endregion
    }



}
